package com.tadpole.music.presenter.me;

import com.tadpole.music.callback.BaseCallback;
import com.tadpole.music.config.Constant;
import com.tadpole.music.config.UrlConfig;
import com.tadpole.music.iView.login.RegisterIView;
import com.tadpole.music.model.base.DataModel;
import com.tadpole.music.model.base.ModelToken;
import com.tadpole.music.presenter.base.BasePresenter;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 修改用户信息
 * Created By 孙岩
 * on 2020/9/27
 */
public class UpdateUserInfoPresenter extends BasePresenter<RegisterIView> {
    /**
     * 修改用户信息
     * @param flag 1表示昵称 2表示头像 3表示生日 4表示性别
     * @param str
     */
    @SuppressWarnings("unchecked")
    public void updateUserInfo(int flag, String str) {
        if (isViewAttached()) {
            String url = UrlConfig.updateUserInfoV2;
            String token = SpUtil.getInstance(getView().getContext()).getString(Constant.TOKEN, "");
            if (flag == 1) {
                DataModel.request(ModelToken.PUT_HEAD_REQUEST)
                        .url(url)
                        .token(token)
                        .paramKey("nickname")
                        .paramValue(str)
                        .execute(new BaseCallback<String>() {
                            @Override
                            public void onSuccess(String data) {
                                try {
                                    JSONObject obj = new JSONObject(data);
                                    if (obj.getInt("code") == 200) {
                                        getView().showResult();
                                    } else if (obj.getInt("code") == 401) {
                                        getView().show401();
                                    } else {
                                        ToastUtils.show(obj.getString("msg"));
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }

                            @Override
                            public void onFailure(String msg) {
                                getView().showErr();
                            }

                            @Override
                            public void onComplete() {
                                getView().hideLoading();
                            }

                            @Override
                            public void onBefore() {
                                getView().showLoading();
                            }
                        });
            }else if(flag==2){
                DataModel.request(ModelToken.PUT_HEAD_REQUEST)
                        .url(url)
                        .token(token)
                        .paramKey("avatar")
                        .paramValue(str)
                        .execute(new BaseCallback<String>() {
                            @Override
                            public void onSuccess(String data) {
                                try {
                                    JSONObject obj = new JSONObject(data);
                                    if (obj.getInt("code") == 200) {
                                        getView().showResult();
                                    } else if (obj.getInt("code") == 401) {
                                        getView().show401();
                                    } else {
                                        ToastUtils.show(obj.getString("msg"));
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }

                            @Override
                            public void onFailure(String msg) {
                                getView().showErr();
                            }

                            @Override
                            public void onComplete() {
                                getView().hideLoading();
                            }

                            @Override
                            public void onBefore() {
                                getView().showLoading();
                            }
                        });
            }else if(flag==4){
                DataModel.request(ModelToken.PUT_HEAD_REQUEST)
                        .url(url)
                        .token(token)
                        .paramKey("gender")
                        .paramValue(str)
                        .execute(new BaseCallback<String>() {
                            @Override
                            public void onSuccess(String data) {
                                try {
                                    JSONObject obj = new JSONObject(data);
                                    if (obj.getInt("code") == 200) {
                                        getView().showResult();
                                    } else if (obj.getInt("code") == 401) {
                                        getView().show401();
                                    } else {
                                        ToastUtils.show(obj.getString("msg"));
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }

                            @Override
                            public void onFailure(String msg) {
                                getView().showErr();
                            }

                            @Override
                            public void onComplete() {
                                getView().hideLoading();
                            }

                            @Override
                            public void onBefore() {
                                getView().showLoading();
                            }
                        });
            }else if(flag==3){
                DataModel.request(ModelToken.PUT_HEAD_REQUEST)
                        .url(url)
                        .token(token)
                        .paramKey("birthday")
                        .paramValue(str)
                        .execute(new BaseCallback<String>() {
                            @Override
                            public void onSuccess(String data) {
                                try {
                                    JSONObject obj = new JSONObject(data);
                                    if (obj.getInt("code") == 200) {
                                        getView().showResult();
                                    } else if (obj.getInt("code") == 401) {
                                        getView().show401();
                                    } else {
                                        ToastUtils.show(obj.getString("msg"));
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }

                            @Override
                            public void onFailure(String msg) {
                                getView().showErr();
                            }

                            @Override
                            public void onComplete() {
                                getView().hideLoading();
                            }

                            @Override
                            public void onBefore() {
                                getView().showLoading();
                            }
                        });
            }
        }
    }
}
